package com.xiaotu.spider.guduo.pageprocessor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.GuDuoNetMoviePlayCountModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.GsonUtils;
import com.xiaotu.common.util.PageUtils;
import com.xiaotu.common.util.UUIDUtils;
import com.xiaotu.server.service.guduo.SupplementGuDuoRecordService;
import com.xiaotu.spider.AUrlPageProcessor;

import us.codecraft.webmagic.Page;
/***
 * 骨朵剧播放量任务
 * @author Mr.zhang
 *
 */
public abstract class GuDuoPlayCountPageProcessor extends AUrlPageProcessor {
	
	@Autowired
	private SupplementGuDuoRecordService supplementGuDuoRecordService;
	
	private String [] start_url = {"https://www.baidu.com/"};
	
	private String play_count_url = "http://ev.guduodata.com/playcount/day/%s?day=%s";
	
	@Override
	public void process(Page page) {
		
		Map<String, Object> extrasMap = page.getRequest().getExtras();
		
		switch (this.getPageDataType(page))
        {
            case "playcount":
                this.playCountProcess(page,extrasMap);
                break;
            default:
            	this.addRequestProcess(page);
            	break;
        }
	}
	
	//获取播放量
	@SuppressWarnings("unchecked")
	private void playCountProcess(Page page, Map<String, Object> extrasMap) {
		
		List<GuDuoNetMoviePlayCountModel> pipeList = new ArrayList<GuDuoNetMoviePlayCountModel>();
		
		String tv_dataid = extrasMap.get("tv_dataid").toString();
		String tvname = extrasMap.get("tvname").toString();
		String json = page.getRawText();
		if (StringUtils.isEmpty(json))
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        Map<String, Object> map = GsonUtils.fromJson(json, Map.class);
        if (map == null || map.isEmpty() || map.get("clicks") == null)
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
		List<List<Double>> clicksList = (List<List<Double>>) map.get("clicks");
		List<String> namesList = (List<String>) map.get("names");
		List<String> datesList = (List<String>) map.get("dates");
        if (clicksList == null || clicksList.isEmpty()
        		|| namesList == null || namesList.isEmpty()
        		|| datesList == null || datesList.isEmpty())
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        for(int i = 0;i<namesList.size();i++){
        	String platFormName = namesList.get(i).toString();
        	List<Double> clicksValueList = clicksList.get(i);
        	for(int j = 0;j<clicksValueList.size();j++){
    			GuDuoNetMoviePlayCountModel model = new GuDuoNetMoviePlayCountModel(
    					UUIDUtils.getId(), tv_dataid, tvname, platFormName, datesList.get(j),
    					clicksValueList.get(j), System.currentTimeMillis()
    					);
    			pipeList.add(model);
        	}
        }
        page.putField(GuDuoNetMoviePlayCountModel.class.getName(), pipeList);
	}

	//添加请求
	private void addRequestProcess(Page page){
		//如果某个剧连续20天左右都无数据 则不进行爬取
		List<Map<String,Object>> list = supplementGuDuoRecordService.getGuDuoNetMovieList(getParam());
		for(Map<String,Object> obj : list){
			Map<String,Object> paramMap = new HashMap<String,Object>();
			String tv_dataid = obj.get("tv_dataid").toString();
			String days = obj.get("days").toString();
			String tvname = obj.get("tvname").toString();
			paramMap.put("tv_dataid", tv_dataid);
			paramMap.put("tvname", tvname);
			if(StringUtils.isNotBlank(tv_dataid) && StringUtils.isNotBlank(days)){
				//1 代表昨日播放量
				PageUtils.addTargetRequest(page, String.format(play_count_url, tv_dataid,1), paramMap);
			}
		}
	}
	
	//查询参数
	public abstract Map<String,Object> getParam();
	
	
	@Override
	protected String[] getStartRequestUrls() {
		return start_url;
	}

	@Override
	protected String getSpiderType() {
		return Constants.SpiderTaskType.TV_GUDUO;
	}

	@Override
	public String getPageDataType(Page page) {
		String url = page.getRequest().getUrl();
		if(url.contains("playcount/day")){
			return "playcount";
		}
		return "addRequest";
	}
	
}
